import random
import json
from typing import Dict, Any, List

# 全局人名列表，用于确保不重复
available_names = [
    "Alice", "Bob", "Charlie", "Diana", "Eve", "Frank", "Grace", "Hank",
    "Ivy", "Jack", "Karen", "Leo", "Mona", "Nina", "Oscar", "Paul","Charlotte",
    "Lucas","Mia","Benjamin","Isabella","James","Sophia","Elijah","Ava","Oliver","Emma","Noah","Olivia","Liam"
]

# 预定义性格集合（可选）
predefined_big5 = []


def generate_profile() -> Dict[str, Any]:
    """
    生成智能体的随机角色属性

    :return: 包含角色属性的字典
    """
    if not available_names:
        raise ValueError("No more unique names available to assign to agents.")

    # 从全局列表中移除并使用一个名字
    name = available_names.pop(random.randint(0, len(available_names) - 1))

    genders = ["male", "female"]
    educations = ["high school", "bachelor", "master", "doctor"]

    # 生成大五人格特质
    if predefined_big5:
    # 从预定义集合中随机选择一个性格
        big5 = random.choice(predefined_big5)
    else:
    # 随机生成大五人格特质（范围：0到1）
        big5 = {
            'O': round(random.random(), 2),  # 开放性 (Openness)
            'C': round(random.random(), 2),  # 尽责性 (Conscientiousness)
            'E': round(random.random(), 2),  # 外向性 (Extraversion)
            'A': round(random.random(), 2),  # 宜人性 (Agreeableness)
            'N': round(random.random(), 2)  # 神经质 (Neuroticism)
        }

    profile = {
        'name': name,
        'gender': random.choice(genders),
        'age': random.randint(18, 65),
        'education': random.choice(educations),
        'big5': big5
    }

    # 将生成的 profile 保存到文件
    #save_profile_to_file(profile, f"D:/agentscope/examples/opinion_simulation/{name}_profile.json")

    return profile



def save_profile_to_file(profile: Dict[str, Any], filepath: str):
    """
    将 profile 保存到文件

    :param profile: 角色属性
    :param filepath: 文件路径
    """
    with open(filepath, "w") as f:
        json.dump(profile, f, indent=4)

def generate_multiple_profiles(num_profiles: int, filepath: str):
    """
    生成多个智能体的角色属性并保存到一个 JSON 文件中

    :param num_profiles: 生成的角色数量
    :param filepath: 保存文件路径
    """
    profiles = []
    for _ in range(num_profiles):
        profiles.append(generate_profile())

    # 将生成的所有 profiles 保存到文件
    with open(filepath, "w") as f:
        json.dump(profiles, f, indent=4)


def generate_basic_prompt(profile):
    """
    生成基础的填充内容（不包含记忆部分）

    :param profile: 用户的profile信息
    :return: 填充后的字符串
    """
    name = profile["name"]
    age = profile["age"]
    gender = profile["gender"]
    education = profile["education"]
    big5 = profile["big5"]

    prompt = f"你的姓名是{name},你是一个{gender}的{age}岁{education}教育程度者。\n" \
             f"你的性格特质如下：\n（开放性{big5['O']}, 尽责性{big5['C']}, 外向性{big5['E']}, \n" \
             f"宜人性{big5['A']}, 神经质{big5['N']}）"

    return prompt


#生成 10 个 profile 并保存到文件
generate_multiple_profiles(30, "D:/agentscope/examples/opinion_simulation/profiles.json")

# 假设文件名为 profiles.json
filename = 'D:/agentscope/examples/opinion_simulation/profiles.json'

# 直接读取文件，假设文件内容已是 Python 数据（比如列表）
with open(filename, 'r', encoding='utf-8') as f:
    profiles = eval(f.read())  # 使用 eval() 将文件内容转换为 Python 数据结构

# # 生成所有profiles的基础信息并保存为字符串数组
prompts = [generate_basic_prompt(profile) for profile in profiles]

# 打印字符串数组
print(prompts)


